# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Alexander Baldeck <alexander@archlinux.org>
# Contributor: Dale Blount <dale@archlinux.org>
# Contributor: Anders Bostrom <anders.bostrom@home.se>

pkgbase=thunderbird
pkgname=(thunderbird)
pkgver=91.1.2
pkgrel=1
pkgdesc='Standalone mail and news reader from mozilla.org'
url='https://www.mozilla.org/thunderbird/'
arch=(x86_64)
license=(MPL GPL LGPL)
depends=(
  glibc gtk3 libgdk-3.so libgtk-3.so mime-types dbus libdbus-1.so dbus-glib
  alsa-lib nss hunspell sqlite ttf-font libvpx libvpx.so zlib bzip2 libbz2.so
  botan libwebp libwebp.so libwebpdemux.so libevent libjpeg-turbo libffi
  libffi.so nspr gcc-libs libx11 libxrender libxfixes libxext libxcomposite
  libxdamage pango libpango-1.0.so cairo gdk-pixbuf2 icu libicui18n.so
  libicuuc.so freetype2 libfreetype.so fontconfig libfontconfig.so glib2
  libglib-2.0.so pixman libpixman-1.so gnupg
)
makedepends=(
  unzip zip diffutils python python-setuptools yasm nasm mesa imake libpulse
  xorg-server-xvfb autoconf2.13 rust clang llvm cbindgen nodejs
  gawk perl findutils libotr
)
options=(!emptydirs !makeflags)
source=(https://ftp.mozilla.org/pub/mozilla.org/thunderbird/releases/$pkgver/source/thunderbird-$pkgver.source.tar.xz{,.asc}
        thunderbird.desktop
        vendor-prefs.js
        distribution.ini
        mozconfig.cfg
        metainfo.patch)
validpgpkeys=(
  14F26682D0916CDD81E37B6D61B7B526D98F0353 # Mozilla Software Releases <release@mozilla.com>
  4360FE2109C49763186F8E21EBE41E90F6F12F6D # Mozilla Software Releases <release@mozilla.com>
)

# Google API keys (see http://www.chromium.org/developers/how-tos/api-keys)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact foutrelis@archlinux.org for
# more information.
_google_api_key=AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM

# Mozilla API keys (see https://location.services.mozilla.com/api)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact heftig@archlinux.org for
# more information.
_mozilla_api_key=16674381-f021-49de-8622-3021c5942aff

prepare() {
  cd $pkgname-$pkgver

  echo "${noextract[@]}"

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  printf "%s" "$_google_api_key" >google-api-key
  printf "%s" "$_mozilla_api_key" >mozilla-api-key
  cp ../mozconfig.cfg .mozconfig
  sed "s|@PWD@|${PWD@Q}|g" -i .mozconfig
}

build() {
  cd $pkgname-$pkgver
  if [[ -n "${SOURCE_DATE_EPOCH}" ]]; then
    export MOZ_BUILD_DATE=$(date --date "@${SOURCE_DATE_EPOCH}" "+%Y%m%d%H%M%S")
  fi
  export MACH_USE_SYSTEM_PYTHON=1
  ./mach configure
  ./mach build
  ./mach buildsymbols
}

package_thunderbird() {
  optdepends=(
    'libotr: OTR support for active one-to-one chats'
    'libnotify: Notification integration'
  )

  cd $pkgname-$pkgver
  DESTDIR="$pkgdir" ./mach install

  install -Dm 644 ../vendor-prefs.js -t "$pkgdir/usr/lib/$pkgname/defaults/pref"
  install -Dm 644 ../distribution.ini -t "$pkgdir/usr/lib/$pkgname/distribution"
  install -Dm 644 ../thunderbird.desktop -t "$pkgdir/usr/share/applications"
  install -Dm 644 comm/mail/branding/thunderbird/net.thunderbird.Thunderbird.appdata.xml \
    "$pkgdir/usr/share/metainfo/net.thunderbird.Thunderbird.appdata.xml"

  for i in 16 22 24 32 48 64 128 256; do
    install -Dm644 comm/mail/branding/thunderbird/default${i}.png \
      "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/$pkgname.png"
  done
  install -Dm644 comm/mail/branding/thunderbird/TB-symbolic.svg \
    "$pkgdir/usr/share/icons/hicolor/symbolic/apps/thunderbird-symbolic.svg"

  # Use system-provided dictionaries
  ln -Ts /usr/share/hunspell "$pkgdir/usr/lib/$pkgname/dictionaries"
  ln -Ts /usr/share/hyphen "$pkgdir/usr/lib/$pkgname/hyphenation"

  # Install a wrapper to avoid confusion about binary path
  install -Dm755 /dev/stdin "$pkgdir/usr/bin/$pkgname" <<END
#!/bin/sh
exec /usr/lib/$pkgname/thunderbird "\$@"
END

  # Replace duplicate binary with wrapper
  # https://bugzilla.mozilla.org/show_bug.cgi?id=658850
  ln -srf "$pkgdir/usr/bin/$pkgname" \
    "$pkgdir/usr/lib/$pkgname/thunderbird-bin"
}

_package_i18n() {
  pkgdesc="$2 language pack for Thunderbird"
  depends=("thunderbird>=$pkgver")
  install -Dm644 thunderbird-i18n-$pkgver-$1.xpi \
    "$pkgdir/usr/lib/thunderbird/extensions/langpack-$1@thunderbird.mozilla.org.xpi"
}

_languages=(
  'af     "Afrikaans"'
  'ar     "Arabic"'
  'ast    "Asturian"'
  'be     "Belarusian"'
  'bg     "Bulgarian"'
  'br     "Breton"'
  'ca     "Catalan"'
  'cak    "Kaqchikel"'
  'cs     "Czech"'
  'cy     "Welsh"'
  'da     "Danish"'
  'de     "German"'
  'dsb    "Lower Sorbian"'
  'el     "Greek"'
  'en-GB  "English (British)"'
  'en-US  "English (US)"'
  'es-AR  "Spanish (Argentina)"'
  'es-ES  "Spanish (Spain)"'
  'et     "Estonian"'
  'eu     "Basque"'
  'fi     "Finnish"'
  'fr     "French"'
  'fy-NL  "Frisian"'
  'ga-IE  "Irish"'
  'gd     "Gaelic (Scotland)"'
  'gl     "Galician"'
  'he     "Hebrew"'
  'hr     "Croatian"'
  'hsb    "Upper Sorbian"'
  'hu     "Hungarian"'
  'hy-AM  "Armenian"'
  'id     "Indonesian"'
  'is     "Icelandic"'
  'it     "Italian"'
  'ja     "Japanese"'
  'ka     "Georgian"'
  'kab    "Kabyle"'
  'kk     "Kazakh"'
  'ko     "Korean"'
  'lt     "Lithuanian"'
  'ms     "Malay"'
  'nb-NO  "Norwegian (Bokmål)"'
  'nl     "Dutch"'
  'nn-NO  "Norwegian (Nynorsk)"'
  'pa-IN  "Punjabi (India)"'
  'pl     "Polish"'
  'pt-BR  "Portuguese (Brazilian)"'
  'pt-PT  "Portuguese (Portugal)"'
  'rm     "Romansh"'
  'ro     "Romanian"'
  'ru     "Russian"'
  'sk     "Slovak"'
  'sl     "Slovenian"'
  'sq     "Albanian"'
  'sr     "Serbian"'
  'sv-SE  "Swedish"'
  'th     "Thai"'
  'tr     "Turkish"'
  'uk     "Ukrainian"'
  'uz     "Uzbek"'
  'vi     "Vietnamese"'
  'zh-CN  "Chinese (Simplified)"'
  'zh-TW  "Chinese (Traditional)"'
)
_url=https://ftp.mozilla.org/pub/mozilla.org/thunderbird/releases/$pkgver/linux-x86_64/xpi

for _lang in "${_languages[@]}"; do
  _locale=${_lang%% *}
  _pkgname=thunderbird-i18n-${_locale,,}

  pkgname+=($_pkgname)
  source+=("thunderbird-i18n-$pkgver-$_locale.xpi::$_url/$_locale.xpi")
  eval "package_$_pkgname() {
    _package_i18n $_lang
  }"
done

# Don't extract languages
noextract=()
for _src in "${source[@]%%::*}"; do
    case "$_src" in 
      *.xpi) noextract+=("$_src") ;;
    esac
done

sha512sums=('f211ce2469f60862b1d641b5e165292d98db53695ab715090034c1ee2be7b04931f8e5e856b08b0c8c789e4d98df291d59283c257a38b556c0b4b0b63baa539f'
            'SKIP'
            'a0061fcb2a7f66061e336a8d95948592f56f4752e56467f14ba63846720ebf845cce7511d1a2637e3b80d5a1ffdaa2fb783fa37195103425ef65222d45372012'
            '6918c0de63deeddc6f53b9ba331390556c12e0d649cf54587dfaabb98b32d6a597b63cf02809c7c58b15501720455a724d527375a8fb9d757ccca57460320734'
            '5cd3ac4c94ef6dcce72fba02bc18b771a2f67906ff795e0e3d71ce7db6d8a41165bd5443908470915bdbdb98dddd9cf3f837c4ba3a36413f55ec570e6efdbb9f'
            '328422adac0cfe6af4ecdcd864004b7ada8f8171aa954fecc23a7e883e90a9bb0848372faa1100440dc754922f965e1e7b98c185aa88df190bff1051d2146c85'
            '7e43b1f25827ddae615ad43fc1e11c6ba439d6c2049477dfe60e00188a70c0a76160c59a97cc01d1fd99c476f261c7cecb57628b5be48874be7cf991c22db290'
            'fe233687299fe0d98820fd65c47e0607e9e5ec3c88e25e38d744a2ce1ab0a58ed1366af37193b8e55482bbd540a6f5cf6406804967b960a7105045cd139f57a5'
            '97d9c92619e9f41f24c792bb8eedd7920682a2a5830c73f48028f38b5e56048bb35703f1aa0b22f2de4f4cff94672805d366b61c8c235c700c12b22a7d0cece7'
            '75aed1e96df9d56be6bde69374d6246e07b0eadc7d300601b0d40ae657c2a5eebe0b3ce66c8a43e23119a612d77905f31bbd0fec80da9ca0232154c9a8a56c2b'
            '3ac5ef8e672d9fbfcd7b1c4dd5bd1e3b6e15602ae0ff50202f792a579f0b880ec319e56a68574beb7d4819949dbe7a77936ddb4d337cc1b58aabb35001716f62'
            '606cbd6f0c71fa7b2adc6ddd6c818f4482fa58c062b2e35888ae69e5b06e76bb32bb96e9e8a4ccea8fe3ade05441c4ef1bc760d4c33c84f42da274d106d7fd3f'
            '14f63603fa77a063d7767ba21bddabd82751f675905007a3918fe16bf07ab428dfcd10b0f56bcec24c5086cd83077a7a45ecd72829cb3f4fd93f35f5e47019f7'
            '41f2c9e901240e6fa7cd68be3cec337b1289df78be4eabcabc19eaa169d7415b5cfbf1dd92e8b5512cf335862184ad442c279ed574d6425f3648d875a9e92155'
            '3e0a8a54789ebb4259413d6c7454db00ae54f0ed07d91654447ade2fd97bfdcd8a89c3f56bcf2dc62b3ef1dbd5c0f339cee23bba853015ecd6ccb69b49f143db'
            'b7579d169cc117b148f6917d8df7029dd00f89a225fbbe2759c395697452bbce95536fd770ee6777f8ed124e94c97e529a10793b3639c68c2a00bc4536a7424d'
            '4168fae69988c6d4419582e0fd0c83462d25a5922ef773640eed644d85c299c80405437bfa38de6e48e288af60cdb8181622ee9b1407aa879b831bce90404088'
            'a13da7ad24731a35f7cde54eea8975fe9d64291fa119467a2c22887d98640c6c977f643ced657a9841721ebaca293a278d8a290f3949a35b383b3fef89b23407'
            '4d3a9e4eac0f89d154964bbfeadab269ae0ecd46cce5026efe34f4efbf0581bc7d24a9b66fe8c71b897f1fbd0020b72d823562afb6bb865ad4ec400cb16d9a2e'
            'f09cae1db0807ee09caf985947b7f226b5ee0be5a737f66dd4886083eb5a2488101c1d8e960439fc0672705038fa36124a67fc42ae5078636b33e951bf5b5b3e'
            '8f9d3b1f9dad86ab58537c1250b4aa4ddab4bc08272697dd10036c30f4f84dcb1086a00175a87824f5bcfe0c1ca86ab7bbc6495939ca9efa91f188da68c45b1e'
            '3568f524501bed0a18c33aaa3dbd3407afc6a29d137a333096c83379fadf19be749eba6d01b94dfb9ecfef178f2ae86d8ad179bf0ff5e50638ddcd45e1d6218b'
            'e451538df81559a848eb9a261b94873c519a436d24309a80e5c8494c6681d77dde8fd304141a9ddab5097d13d3790532b012855e7214868630ba955a1fd5b74a'
            '8da292e3ca317c62c57793cab16b89e18df5767290f06675a08f42e6360f1896c356d30877dadb957780e5797ba7c319c68b2352f3942c1dc3751234b9512597'
            'bc7c69fbe801258d44ea6ebcb82c6c80e4f134f08da1909e41a9904e4c4715b5ad81c9deac1f734f599378f4944032a29ddfeedc49261126138be1125ee617f0'
            '3fb6ec3e750b32774b4017889450f3f131668279a548f7e1cc8da6b5ecebed1ac18728ad0c766fc83e257e809ef4c6d68790167c22d7ef13afdb2f5a70aa1e2c'
            'e69c9b5d379feeee399a651c8f229d7025c734d5b244f0ef1051f3ecaf12f4ce74bb1dddc2024a88c1f74ffff9e720bb8dbd6c5ff50f6670a2f5b53484e61329'
            '537035b24965935f1157a59564fb3978fff49833a2a3c7a1de09ac7fc3008332191a4b3f11510c7942525868fd8a2d0d690e5b16310fdedfaa56b76853183106'
            '7abf38fd4eea006528789c107220a9af9f97d703809457f120a987d9d44b8da015fc15a3adfd5d3778a01a87776fcab74bf1c1e0daa64839db07154be2c957d1'
            '7d11ca6a076e7a6e544861f3e9132b8ab5a65cbab7ef6538f0d2b1148813a4d9e10aaa585c3503049d4fbb74a8d0e1c3faf5edd9b669426434aa52c7ab1ba614'
            '6352017a8f4e2e7647d8b12b0d3d2f82d1095bf8c1f294bac709db23a65352ddaffa97b026866fef3c0a9794b2e555f1b95bd92d0cc8da256238f1159ee0113d'
            'a65b7ee6bbc632b82047c242a45aa283c727239a5a10b298ce58746929f8bd497b738bdae990d8c246f3b0309f325d2c48aaf5619a1dacee5ee82be76bdf59cb'
            '4ea736050a32fa09108341d0c93ea321c42247398310b2a243c941470a0a67053c31949dc88ffc0090c0442284bf0b4eb387d924e6e1221b67f8a83ac748f034'
            '786da9c3a6b4c9a045cbbf49db8f73525149538d133ac4f69f166ebec38ea0e303eae120e143e8b58182f7d096b49dd2b5645d0d61ec355251847cd4dc65005c'
            '6cdd883b6394406a7d8b6d55c2fd9451f0da9b7c933d7705b8a034ece13865295df79f7e3c84b3b23e726fff10c669d590f8c5c65a95d8f9f15b42637e69e06d'
            'd5f2346d312910b3ba4f6e941604c5d1b2b4acfd9e30ffbc45d546be64e43bfb13f52cb10427bd09313997717e8af4dc09dfc18d24cce489eeff0a99565be597'
            '310e4bba9e7d3dd233576090508ed7b2792b4c35fc1d120c99e37bc43e5ad0d8ba1ba738ded79c529bdb91ea0c6383f68e4272b9c2b453709553112fcdba18e3'
            'd4c223ef864b98087e19dcfa8ac00a54c70b00664c4eaeb5c24b1635d892d784762920a8c2ea8435ca74d58c0d259da1d46ad1cd9aa914f1afbd3c0b29a18248'
            '53fdc04023b4e13cd264aad1c09855a06c3053b2146409e10199083b5ecd0ee3a81cb5926ad2132f29781772aef89a843c88ee8a6426766fb5381bc1a2f6ce21'
            '875421007b3622569f2726b0154ab39b84eee029ea896f9723558a513ed4c4d6cce549515c5f47b064a30d4896ee7a3db363be7f9b7ac0d83436ad2a191315cf'
            '6c771ebace3ca11ee123fc3f74bf5fa7aadda4889583d0142e1e59dc5567cdbbf77263a2b228ac0c181cb34eb2228c2ab016b6cc010187bd4e99829803f83ba7'
            '1c65f926e42e95b2bd2972331d942181dd597550ac2b543beb1defa9cdf0d4f75f7521912bd954dd566caad5b28110087b8e07683dc02ebdf4f3c95b35404da6'
            'dd1b00c6643c4e465d622ddccbcda8a8d83166524f0026443879cbfcdc80c0805705ae1675d9af759dd0b91710ebab19fda8b8f252faefde68674238ba41b2d8'
            '3a97d12f5fa2fcec889bbcf6715f22797a57e02869e67036347c5bf4b2b56e36fda404b8c7824330c5ad8915f264995df96747e491666bdf081b4b5a6c7b7f7f'
            'd319690de832a45f15021cacc3441e03ff1ef0ebe8beff9f959ffff97bcfca46dc03a9b6bd98e59eaece38a5085ab37f535cd1335f0a20506a495bdc7e4bcad0'
            '4202412e97de11ab6bd131475eb0005d9fface4dd402d505e85a9b3102d234d01ffebf3c2fbb8aaeb995e62abbfa17c08545c57c5ab43856df929fd8bb1231ae'
            '05af09d486310fb66ffb23bf70375b16449127f12022981f46ceb35849fdad5d96df3de53ed009934f6be337a866c6795853505991c7bbef09f321211dc82713'
            '9a2e0b7b980f2a0fba50922e7d8455799b981b1b1ae4ec197b70d7982dfacd5d4ee5cfd393d031af7fea6477a397694fe91723396d7d2fe123ce51178ab1f3cd'
            '8e5cebb9530cc0d803e5685ba9b476284098123bbc186d07ba49993c0d986d47e35b30e6601b9273dcf10d7c1172690bcf4780c74f7753e3ec1334f6a984103b'
            '512135ced1be1f77a3d6482fa1b2798d90e6ad0b8bd225edbbbcbbee578adf5bfa1dfea00c9cf776bfdfdfc3df101e03d23f8d92c3b75927a59edf122abee3b7'
            '8bee3198ffa595bcf09451e1a46dd3df1aef68b5db3e49c552d5f9bbd027457465795e13efb7f6a4ec64f747c26ebfda14c83b3e0d7df5d803d910fd9c1338ac'
            '62281f22a8b8b9d91ecfabcca7d64117fdfc0c859b3acd42203ebbed9ad2fb32a49c5eb559c9e06861442d9372a5836c49e515c905faf315a3c751ee9728cad7'
            '76a1ce0503ff56056c603ba758147b678fe0d579f51a0fca1d8c844b49922edcf897ff93115037625f22c014410322ec5e0a9b8224f62f69d770da484fe8d92e'
            '2e82e3f8c23f14264e7ae7b0a631c85b734326edb006ac6fa8c44abb210bb0462a035319c7e7cc71611d28447e9821239284bde87e8ef59d9033bd24ddcc53fa'
            '96994a1999ce19b39b03a7c4968f0f76b583c0a2316290a2fc9508dff1103fa0ecbdcef6b9e9b2dbd818b263c6cc30dad6ecfc81014e8d562a270a9f3b74c2a8'
            '23db970eb1a35acc9b6ff2099570938795dc9e303e3080323d219bb1280bebd20d76e201da6984cd339765730407161ccecc30d795a29efb4323149c1100e1bd'
            'bb80ce8a5233437cd21230de980603556069ca9644e83d537c8489d6c025a968d28e27d5bef6b185d8388885f5694ba5d490c0d86a561eb4de6834bae105503c'
            '15575651bf962bf54b5d7c3bfa98e8569e7e86d9b3619eee19d36331aa5b26f156a107688b77bd2a8c36c3feaf602586ce7028fe0d5d0c84cd8fed5408890a1b'
            '7859b8206b1da88927b13ba725a16d3d639d46471b24c1ba4d21deba0970886bb5cb0905d91303fdd701d44d163a661ae275682030cda6b69c10874e2a2582bb'
            'ba70e27dc66f496bb478487ab2f2f3b24f6d8f041b41f591d82c2b4847af44e9cef5fae81f04967eb790cb94549451ee3715a81b71759a593217a9c9fd01b7eb'
            'e9f06437ad084491d65f20514861f54ab282914772610c628d8fe7386ef33c103adc505eb657f421f9264826f9f0d59db5083b4f0505e7ccc1490488c60712c7'
            '53bc013d695cd66535f1326235c7f4032b025576dd520bd9cb0bf63ad8db5e2e049032ff0394cc7bfa3ff52c88d878a30501c0fc0aed1717e6772dacbb9a749a'
            'f0499184ad996f78842b78408ec9fbede4f1e1ad7b2bc078048ae9e3b10b1c2242f4af2669e3d1b497e9f61f1c5b728fe0f9fbc2311a4a0027c1fa92c0237c12'
            '61444d9ae4dd342acce213d8204af12b6649ee5a845e0f9e26f4f80af65d03deb79a5a5d3a459416f9ac8e3c953d70dd2ab8bf30e921e4505fed4ab706cc273b'
            '45d393f24bf151631bc5169569cad8fae93edebd823d0735fa461ff3d7157f1d4ccbdcd5d9b7fc83b66f6e04fb41f9ed14926b2bf0fbd03e2e44d669a08642a0'
            'd643e452ac2cbec3ceb04c04422c388365a1e279bf0a584f96dd3c6d69c6ade8900ab11333ffbbb73d222f3a9c4055117838cbc7952cb185239ccb284a7a438a'
            '293641b40d8c9aafab0b6613eb834df5f6826a07c9e0eec56e911014e78c8c7f51cb6bbe82f03a69e9e6d8c682518b82f844ef622eeb6bb15eb5fd17d3bcb367'
            '471e45d92b58eb2995f90a404472180a67cc9796b34f641a6f7d451e5cbb71659aab0a82d75cd98e1e22a0e46b7632e100adf9432b91b2f6a80aac1812efaf65'
            '6da9629097e5fe8cccc206ee5757aacfb6773bbad36b31c28130d49b2567cf8ef4020c368b7a5edaf27bfeb6a6bb57de50f3f0c427d6d78ffc78a2eb9ab09f6e'
            '177386e71d50b36bab6beb79cd4b4dd0c5bc66c516f2df9b8c1900b978079f76bed769df78b9c941856928494068b15f79c38f13e3a0615c19a4223ce83cf7a1')

# vim:set sw=2 et:
